package com.imooc.shell.config;

import me.chanjar.weixin.mp.api.WxMpConfigStorage;
import me.chanjar.weixin.mp.api.WxMpInMemoryConfigStorage;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

@Component
public class WechatMpConfig {
    @Autowired
    private WechatAccountConfig wechatAccountConfig;

    @Bean
    public WxMpService wxMpService() {
        WxMpService wxService = new WxMpServiceImpl();// 实际项目中请注意要保持单例，不要在每次请求时构造实例，具体可以参考demo项目
        wxService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxService;
    }

    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {

        WxMpInMemoryConfigStorage wxMpInMemoryConfigStorage = new WxMpInMemoryConfigStorage();
        wxMpInMemoryConfigStorage.setAppId(wechatAccountConfig.getMpAppId()); // 设置微信公众号的appid
        wxMpInMemoryConfigStorage.setSecret(wechatAccountConfig.getMpAppSecret()); // 设置微信公众号的app corpSecret
//        wxMpInMemoryConfigStorage.setToken("..."); // 设置微信公众号的token
//        wxMpInMemoryConfigStorage.setAesKey("..."); // 设置微信公众号的EncodingAESKey
        return wxMpInMemoryConfigStorage;
    }
}